/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package syncmellariusnote;

import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.comm.CommPortIdentifier;
import javax.comm.ParallelPort;
import syncmellariusnote.helpers.ConnectorConfigurator;

/**
 *
 * @author Vitor
 */
public class Receptor {

    public final static int FILE_SIZE = 6358176;
    int bytesRead;
    int current = 0;
    FileOutputStream fos = null;
    BufferedOutputStream bos = null;
    String FILE_TO_RECEIVED;
    int baseAddress = 0x378;
    //LPT Port
    static Enumeration<?> portList;
    static CommPortIdentifier portId;
    ParallelPort parallelPort;
    
    Receptor (){
        
    }
    
    public void receiveFile(String template) {
        FILE_TO_RECEIVED = template;
        try {
            DataInputStream is = new DataInputStream( ConnectorConfigurator.getClientConnection().getInputStream());
            fos = new FileOutputStream(FILE_TO_RECEIVED);
            bos = new BufferedOutputStream(fos);
            
            bytesRead = is.readInt();
            byte[] mybytearray = new byte[bytesRead];
            is.readFully(mybytearray);
            bos.write(mybytearray, 0, bytesRead);
            bos.flush();
            System.out.println("File " + FILE_TO_RECEIVED
                    + " downloaded (" + current + " bytes read)");

            if (fos != null) {
                fos.close();
            }
            if (bos != null) {
                bos.close();
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Receptor.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Receptor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

}
